package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.entity.HouseEntity;
import com.sand.entity.HouseattachmentEntity;
import com.sand.entity.TAuthorityEntity;
import com.sand.service.IHouseService;
import com.sand.service.IUserInfoService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "house")
public class HouseController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/1/001 11:21
 */
    //HouseService接口
    @Resource(name = "houseService")
    private IHouseService houseService;
    //IUserInfoService接口
    @Resource(name = "iUserInfoService")
    private IUserInfoService userInfoService;

    //分页查询房产
    @RequestMapping(value = "queryHouseLimit")
    public void queryHouseLimit(int limit, int page, int showWi, String condition, HttpServletRequest request, HttpServletResponse response, Model model)throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = houseService.queryHouseLimit(limit, page,showWi, condition);
        switch (showWi){
            case 1://后台数据查询
                ModelTest<List<HashMap<String, Object>>> listData = new ModelTest<List<HashMap<String, Object>>>("OK",pm.getList(),0, pm.getPagecount());
                response.getWriter().write(JSON.toJSON(listData).toString());
                break;
            case 2://前台首页
                response.getWriter().write(JSON.toJSON(pm.getList()).toString());
                System.out.println(JSON.toJSON(pm.getList()).toString());
                break;
            case 3://前台搜索页
                response.getWriter().write(JSON.toJSON(pm).toString());
                System.out.println(JSON.toJSON(pm.getList()).toString());
                break;
        }
    }

    //添加房源
    @RequestMapping(value = "addHouse")
    public void addHouse(@RequestParam(value = "upLoad") MultipartFile file,@RequestParam(value = "upLoads") MultipartFile[] files, String data,HttpServletRequest request, HttpServletResponse response)throws Exception{
        boolean bln = houseService.addHouse(file, files,data,request);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //删除房源
    @RequestMapping(value = "delHouse")
    public void delHouse(int rid, HttpServletResponse response)throws Exception{
        boolean bln = houseService.delHouse(rid);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }

    //根据市id查询条件
    @RequestMapping(value = "queryOptionById")
    public void queryOptionById(int id,HttpServletResponse response, Model model)throws Exception{
        HashMap<String, Object> map = houseService.queryOptionById(id);
        response.getWriter().write(JSON.toJSON(map).toString());
    }

    //搜索条件查询
    @RequestMapping("byid")
    public void queryOptionById(HttpServletResponse response,int id) throws IOException {
        response.setCharacterEncoding("utf-8");
        HashMap<String, Object> stringObjectHashMap = houseService.queryOptionById(id);
        System.out.println("---------------遍历userShow------------");
        for(Map.Entry<String, Object> entry : stringObjectHashMap.entrySet()) {
            System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
        }
        response.getWriter().write(JSON.toJSON(stringObjectHashMap).toString());
    }

    //根据房产id查询所有信息
    @RequestMapping(value = "queryHouseById")
    public ModelAndView queryHouseById(int rid,int showWi,HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new ModelAndView();
        HashMap<String, Object> houseShow = houseService.queryHouseById(rid);
        HashMap<String, Object> userShow = userInfoService.queryUserInfoById(Integer.parseInt(houseShow.get("userId").toString()));
        List<HouseattachmentEntity> imgsShow = houseService.queryHouseImgs(rid);
        mv.addObject("houseShow",houseShow);
        mv.addObject("userShow",userShow);
        mv.addObject("imgsShow",imgsShow);
//        System.out.println("---------------遍历houseShow------------");
//        for(Map.Entry<String, Object> entry : houseShow.entrySet()) {
//            System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
//        }
//        System.out.println("---------------遍历userShow------------");
//        for(Map.Entry<String, Object> entry : userShow.entrySet()) {
//            System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
//        }
//        System.out.println("---------------遍历imgsShow------------");
//        for (HouseattachmentEntity u:imgsShow){
//            System.out.println(imgsShow);
//        }
        switch (showWi){
            case 1://后台数据展示
                mv.setViewName("/layui/page/table_house_show");
                break;
            case 2://前台数据展示
                if(request.getSession().getAttribute("drom")!=null){
                    mv.addObject("boo",userInfoService.all((Integer) request.getSession().getAttribute("drom1"),rid));
                }
                mv.setViewName("/renting/pages/onther/details");
                break;
        }
        return mv;
    }
    //房产审核
    @RequestMapping(value = "updateHouseStatus")
    public void updateHouseStatus(int rid,int status,HttpServletResponse response)throws Exception{
        boolean bln = houseService.updateHouseStatus(rid, status);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据userId分页查询房东发布的房源信息
    @RequestMapping(value = "queryHouseByUserIdLimit")
    public void queryHouseByUserIdLimit(@RequestParam(value = "limit") int limit, @RequestParam(value = "page") int page,@RequestParam(value = "condition") String condition, HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = houseService.queryHouseByUserIdLimit(limit,page,condition);

        response.getWriter().write(JSON.toJSON(pm).toString());
        System.out.println(JSON.toJSON(pm).toString());
    }

    //=======================lyl=======================

    //ajax请求房源信息根据房屋id获取
    @RequestMapping("/getHouse")
    public void getHoseMsg(@RequestParam("hid")int hid,
                           HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        //设置传参对象
        JSONObject json = new JSONObject();

        HouseEntity house = houseService.queryHouseData(hid);//获取房子信息
        if(house!=null){
            json.put("house",house);
            List list = houseService.queryHouseImgs(house.getRid());//获取房子图片信息
            if(list!=null&&list.size()>0){
                HouseattachmentEntity houseattachmentEntity = (HouseattachmentEntity) list.get(0);
                if(houseattachmentEntity!=null){
                    json.put("houseImg",houseattachmentEntity);
                    json.put("statecode",0);
                    response.getWriter().write(json.toJSONString());
                }
            }
        }else{
            response.getWriter().write("数据获取失败");
        }
    }




}
